package ${package.ServiceImpl};


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import ${superServiceImplClassPackage};
import com.wang.cloud.common.core.exception.ServiceException;
import com.wang.cloud.common.mysql.utils.PageUtils;
import ${package.Parent}.domain.co.${entity?replace('Entity', '')}AddCo;
import ${package.Parent}.domain.co.${entity?replace('Entity', '')}PageCo;
import ${package.Parent}.domain.co.${entity?replace('Entity', '')}UpdateCo;
import ${package.Parent}.domain.vo.${entity?replace('Entity', '')}PageVo;
import ${package.Parent}.domain.vo.${entity?replace('Entity', '')}Vo;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
<#if generateService>
import ${package.Service}.${table.serviceName};
</#if>
import ${package.Parent}.convert.${entity?replace('Entity', 'Convertor')};
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()<#if generateService>, ${table.serviceName}</#if> {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><#if generateService> implements ${table.serviceName}</#if> {

    @Resource
    private ${entity?replace('Entity', 'Convertor')} convertor;

    @Override
    public IPage<${entity?replace('Entity', 'PageVo')}> page(${entity?replace('Entity', 'PageCo')} pageCo) {
        return this.lambdaQuery()
    <#-- ----------  BEGIN 字段循环遍历  ---------->
    <#list table.fields as field><#assign currPropertyName="${field.propertyName?cap_first}"/>
                .eq(ObjectUtil.isNotEmpty(pageCo.get${currPropertyName}()), ${entity}::get${currPropertyName}, pageCo.get${currPropertyName}())
    </#list>
                .select(${entity}::getId
    <#list table.fields as field><#assign currPropertyName="${field.propertyName?cap_first}"/>
                        , ${entity}::get${currPropertyName}
    </#list>
                )
                .page(PageUtils.getPage(pageCo))
                .convert(convertor::entityToPageVo);
    }

    @Override
    public ${entity?replace('Entity', 'Vo')} getInfo(Long id) {
        ${entity} entity = this.getById(id);
        return convertor.entityToVo(entity);
    }

    @Override
    public Long add(${entity?replace('Entity', 'AddCo')} addCo) {
        ${entity} entity = convertor.addCoToEntity(addCo);
        this.save(entity);
        return entity.getId();
    }

    @Override
    public void update(${entity?replace('Entity', 'UpdateCo')} updateCo) {
        ${entity} oldEntity = this.getById(updateCo.getId());
        if (Objects.isNull(oldEntity)) {
            throw new ServiceException("数据不存在或无权限");
        }
        ${entity} entity = convertor.updateCoToEntity(updateCo);
        this.updateById(entity);
    }

    @Override
    public void del(Long id) {
        this.removeById(id);
    }
}
</#if>
